﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.CongNo
{
    public partial class CapNhatSoTienTra : Form
    {
        private Int64 idcus = -1;
        private Int64 thangnamno = -1;
        public CapNhatSoTienTra(Int64 id,Int64 _thangnamno)
        {
            InitializeComponent();
            idcus = id;
            thangnamno = _thangnamno;
        }

        private void CapNhatSoTienTra_Load(object sender, EventArgs e)
        {
            clCongNo clcongno = new clCongNo();
            DataTable dtcongno = clcongno.chitietcongno(idcus, thangnamno);
            if (dtcongno != null && dtcongno.Rows.Count > 0)
            {
                numberthang.Value = int.Parse(dtcongno.Rows[0]["thangNo"].ToString());
                numberNamtra.Value = int.Parse(dtcongno.Rows[0]["namNo"].ToString());
                txtTongTien.Text = dtcongno.Rows[0]["tongtien"].ToString();
                txtSotientra.Text = dtcongno.Rows[0]["soTienTra"].ToString();
                txtConno.Text = dtcongno.Rows[0]["soTienCon"].ToString();
                dateTimePickerNgayTra.Text = dtcongno.Rows[0]["ngayTra"].ToString();
            }
        }

        public delegate void capnhatcongno(int outid);
        public capnhatcongno MyFunction;

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            if (MyFunction != null)
            {
                Int64 tongtien = Int64.Parse(txtTongTien.Text.Trim());
                Int64 tientra = Int64.Parse(txtSotientra.Text.Trim() == "" ? "0" : txtSotientra.Text.Trim());

                string ngaytra = dateTimePickerNgayTra.Value.ToString("dd/MM/yyyy");

                if (tongtien >= tientra)
                {
                    clCongNo congno = new clCongNo();
                    int outid = congno.trano(idcus, tientra, thangnamno, ngaytra);
                    MyFunction(outid);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Số tiền trả lớn hơn tổng tiền cần trả");
                }
            }
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSotientra_TextChanged(object sender, EventArgs e)
        {
            Int64 tongtien = Int64.Parse(txtTongTien.Text.Trim());
            Int64 tientra = Int64.Parse(txtSotientra.Text.Trim() == "" ? "0" : txtSotientra.Text.Trim());

            Int64 tiencon = tientra - tongtien;
            txtConno.Text = tiencon.ToString();
        }
    }
}
